package com.yearno.common.utils

import java.math.BigDecimal
import java.text.DecimalFormat

enum class DigitalUtil {

    INSTANCE;

    /**
     * 判断字符串是否是数字
     */
    fun isNumber(value: String): Boolean {
        return isInteger(value) || isDouble(value)
    }

    /**
     * 判断字符串是否是整数
     */
    private fun isInteger(value: String): Boolean {
        return try {
            value.toInt()
            true
        } catch (e: NumberFormatException) {
            false
        }
    }

    /**
     * 判断字符串是否是浮点数
     */
    private fun isDouble(value: String): Boolean {
        return try {
            value.toDouble()
            value.contains(".")
        } catch (e: NumberFormatException) {
            false
        }
    }

    fun format(money: String?): String {
        return format(money?.toDoubleOrNull())
    }

    fun format(money: Double?): String {
        return try {
            val decimalFormat = DecimalFormat("#,##0.00")
            val format = decimalFormat.format(money)
            format
        } catch (e: Exception) {
            "0.00"
        }
    }

    fun formatYuan(money: String?): String {
        return formatYuan(money?.toDoubleOrNull())
    }

    fun formatYuan(money: Double?): String {
        return "￥${format(money)}"
    }

    fun formatDouble(double: Double?): String {
        return if (double == null) {
            "0.00"
        } else {
            return BigDecimal(double).setScale(2).toPlainString()
        }
    }
}